package managedbeans.administrator;

import entities.Systemobserver;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import langresources.ResourcesController;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.LineChartSeries;
import utils.dataaccess.DBInterface;



@ManagedBean(name="eventChartBean")
@ApplicationScoped
public class EventChartBean implements Serializable{
    
    private CartesianChartModel categoryModel;  
  
    private CartesianChartModel linearModel;  
    
    private int maxRate = 1000;
  
    public EventChartBean() {  
        createCategoryModel();  
    }  
  
    
    private void createCategoryModel() {  
        categoryModel = new CartesianChartModel();  
        
        ChartSeries events = new LineChartSeries(); 
        ChartSeries users  = new LineChartSeries();
        events.setLabel(ResourcesController.extractResource("SYSTEM_EVENTS"));  
        users.setLabel(ResourcesController.extractResource("USERS")); 
  
        ArrayList<Systemobserver> statistics = DBInterface.getStatistics();
        
        for(Systemobserver statistic : statistics){
            events.set(statistic.getDatestamp().toString(), statistic.getEvents());  
            users.set(statistic.getDatestamp().toString(), statistic.getUsers()); 
            if(statistic.getEvents() > maxRate)
                 maxRate = statistic.getEvents();
        }
        categoryModel.addSeries(events);  
        categoryModel.addSeries(users);

        Iterator<FacesMessage> i = FacesContext.getCurrentInstance().getMessages();
        while(i.hasNext()){
            System.out.println(i.next().getDetail());
        }
    }  
  
    public int getTodaysEventCount(){
        DBInterface.getStatistics(new Date());
        return DBInterface.getStatistics(new Date()).getEvents();
    }
    
    public int getTodaysUsersCount(){
        DBInterface.getStatistics(new Date());
        return DBInterface.getStatistics(new Date()).getUsers();
    }
    
    
    public CartesianChartModel getCategoryModel() {  
        return categoryModel;  
    }  
  
    public CartesianChartModel getLinearModel() {  
        return linearModel;  
    }

    public int getMaxRate() {
        return maxRate;
    }
   
}
